package com.oi.eksekutif.dao.impl;

import com.oi.eksekutif.dao.TagihanDAO;
import com.oi.eksekutif.domain.Tagihan;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Deny Prasetyo
 * Date: Apr 21, 2010
 * Time: 1:28:30 AM
 */
@Repository
@Transactional
public class TagihanDAOImpl implements TagihanDAO {

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    @Override
    public long count() {
         return (Long)this.entityManager.createQuery("SELECT count(o) FROM Tagihan o").getSingleResult();
    }

    @Override
    public List<Tagihan> findAll() {
        return this.entityManager.createQuery("SELECT o FROM Tagihan o").getResultList();
    }

    @Override
    public List<Tagihan> findEntries(int firstResult, int maxResults) {
        return this.entityManager.createQuery("SELECT o FROM Tagihan o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Override
    public Tagihan find(Long id) {
        return entityManager.find(Tagihan.class,id);
    }

    @Override
    public void persist(Tagihan o) {
        this.entityManager.persist(o);
    }

    @Override
    public void merge(Tagihan o) {
        this.entityManager.merge(o);
    }

    @Override
    public void flush() {
        this.entityManager.flush();
    }

    @Override
    public void remove(Tagihan o) {
        this.entityManager.remove(this.entityManager.merge(o));
    }
}